<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>闭包</title>
</head>
<body>
	<script type="text/javascript">
		// 1： 生命周期
		let fnc = () => {
			let val = 1;
			return () => {
				val++
				console.log(val);
			}
		}
		let f = fnc();
		f();
		f();
		f();

		//1 缓存变量
		//2 箭头函数中的arguments 指向外层的arguments
		
		// let mult = () => {
		// 	let args = Array.prototype.join.call(arguments, ',');
		// 	console.log(args);
		// }
		let mult = function (){
			let cache = {};
			let varcalculate = function(){ //  calculate  
			  	var a=1; 
			  	for(var i=0, l = arguments.length; i < l; i++){
					a = a * arguments[i]; 
				}
				return a; 
			};
			return function () {
					let args = Array.prototype.join.call(arguments, ',');
					console.log(arguments);
					console.log(args);
					if ( args in cache ){
						return cache[ args ];
						 }
					return cache[ args ] = varcalculate.apply( null, arguments );
				}
		}();
		console.log(mult(1,2,3,1,2,3))
		console.log(mult(1,2,3))

		// 命令模式
		+function () {
			let Tv = {
				open:  () => {
					console.log('打开电视');
				},
				close: () => {
					console.log('关闭电视');
				},
			}
			let creatCommand = (action) => {
				let doOpen = () => {
					return action.open();
				}
				let doClose = () => {
					return action.close();
				}

				return {
					doOpen: doOpen,
					doClose: doClose
				}
			}
			let actions = creatCommand(Tv);
			actions.doOpen();
		}
		/*
			高阶函数使用：
			1，回调函数，
			2，返回一个函数 
		*/ 
		+function () {
			let arr1 = [5,1,3,3,7,8]
			let arr2 = [5,1,3,3,7,8]
			arr1.sort((a, b) => { //小到大
				return a - b;
			})
			arr2.sort((a, b) => { // 大到小
				return b - a;
			})
		}

	</script>
</body>
</html>